package com.example.manpower.Server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.manpower.Dto.PositionStats;
import com.example.manpower.Entity.Position;
import com.example.manpower.Server.PositionService;
import com.example.manpower.Mapper.PositionMapper;
import com.example.manpower.Util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
* @author asus
* @description 针对表【position】的数据库操作Service实现
* @createDate 2024-10-11 09:56:26
*/
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService{
    @Autowired
    private PositionMapper positionMapper;
    @Override
    public Result select(PositionStats positionStats) {
        List<Position> positions = positionMapper.selectList(null);
        int total =positions.size();
        int start = (positionStats.getPage()-1)*positionStats.getSize();
        positionStats.setStart(start);
        List<PositionStats> list = positionMapper.select(positionStats,1);
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("list",list);
        hashMap.put("total",total);
        return new Result(200,"查询成功",hashMap);
    }
    @Override
    public Result add(Position position) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("position_name",position.getPositionName());
        Position position1 = positionMapper.selectOne(queryWrapper);
        if (position1 != null) {
            return new Result(400,"该职位已经存在",null);
        }
        int num = positionMapper.insert(position);
        return new Result(200,"添加职位成功",num);
    }
    @Override
    public Result updateOne(Position position) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("position_id",position.getPositionId());
        int num = positionMapper.update(position, queryWrapper);
        return new Result(200,"修改职位成功",num);
    }
    @Override
    public Result delete(Position position) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("position_id",position.getPositionId());
        int num = positionMapper.delete(queryWrapper);
        return new Result(200,"删除职位成功",num);
    }

    @Override
    public Result selectall(Position position) {
        List<Position> positions = positionMapper.selectList(null);
        return new Result(200,"查询成功",positions);
    }


    //通过职位id查找职位名字，返回给岗位管理
    @Override
    public String selectpositionName(int positionid) {
        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("position_id",positionid);
        Position position = positionMapper.selectOne(queryWrapper);
        String positionName = position.getPositionName();
        return positionName;
    }


    @Override
    public int selectdepartmentId(int positionid) {

        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("position_id",positionid);
        Position position = positionMapper.selectOne(queryWrapper);
        Integer departmentId = position.getDepartmentId();
        return departmentId;
    }

}




